<?php
namespace Timer\Controllers;


use GatewayWorker\Lib\Gateway;
use Timer\Models\Dispatcher;
use Timer\Module\Logger;
use Timer\Module\Tools;

class Process{
	
    /**
     * 当客户端连接时触发
     * 如果业务不需此回调可以删除onConnect
     * 
     * @param int $client_id 连接id
     */
    public static function processOnConnect($client_id)
    {
	    //设备登陆
	    //Gateway::sendToClient($client_id, '{"result":$client_id}');
	    //var_dump($client_id);
    }


    /**
     * 当用户断开连接时触发
     *
     * @param int $client_id 连接id
     * @param $port
     */
    public static function processOnClose($client_id , $port)
    {
        //断开
    }


    /**
     * 数据处理入口
     *
     * @param int $client_id 连接id
     * @param string $message 具体消息
     * @param $port
     * @return bool
     */
    public static function processOnMessage($client_id, $message, $port)
    {
	    if($message == "@"){
		    Gateway::sendToClient($client_id, "@");
	    } else if(false != $message && $message != "") {
		    //判断request格式是否正确
            $req_ary = Tools::getInstance()->checkupRequestFormat($message);
//            var_dump($req_ary);
		    if(is_array($req_ary) && !isset($req_ary['result'])) {
			    $req_ary['client_id'] = $client_id;
			    $result = Dispatcher::dispatcherOpt($req_ary);
		    } else {
			    $result = $req_ary;
		    }
//            Logger::getInstance()->debug($message);
//		    if($result != OPT_CONST::NO_RETURN) {
			    //格式化返回结果
			    //$ret_result = Process_Model::getModelObject()->formartReturn($result,$request_params);

	    	    //发送返回结果给client
			    //Gateway::sendToClient($client_id, $ret_result);
//		    }
	    }
	    return true;
    }

}	
